package com.joycessh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.joycessh.domain.Result;
import com.joycessh.domain.ResultCode;
import com.joycessh.domain.crawl.SeedUrl;
import com.joycessh.service.SeedUrlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Joycessh
 * @Description
 * @Date 5/9/19 5:11 PM
 */
@Slf4j
@Api(description = "SeedUrl增删改查接口")
@RestController
@RequestMapping("/api/v1/seed/url")
public class SeedUrlController {

    @Autowired
    private SeedUrlService seedUrlService;

    @ApiOperation(value = "添加", notes = "根据前台输入的信息添加SeedUrl到数据库")
    @PostMapping("")
    public Result addSeedUrl(@RequestBody SeedUrl seedUrl) {
        if (seedUrlService.save(seedUrl)) {
            log.info("添加SeedList的自增主键为：" + seedUrl.getId());
            return Result.success().addData("SeedUrl", seedUrl);
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "删除SeedUrl", notes = "根据Id删除数据")
    @DeleteMapping("/{id}")
    public Result removeSeedUrl(@PathVariable Integer id) {
        if (seedUrlService.delete(id)) {
            return Result.success();
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "更新SeedUrl")
    @PutMapping(value = "")
    public Result updateSeedUrl(@RequestBody SeedUrl seedUrl) {
        if (seedUrlService.update(seedUrl)) {
            return Result.success();
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "获取所有的SeedUrl数据", notes = "根据seedListId获取所有的SeedUrl")
    @GetMapping("/{currentPage}/{pageSize}/{seedListId}")
    public Result findAllSeedUrl(@PathVariable Integer seedListId,
                                 @PathVariable Integer currentPage,
                                 @PathVariable Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<SeedUrl> lists = seedUrlService.findAll(seedListId);
        PageInfo<SeedUrl> pageInfo = new PageInfo<>(lists);
        return Result.success().addData("seedUrls", pageInfo);
    }
}
